package com.example.idachuappone.promptly.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import antistatic.spinnerwheel.adapters.NumericWheelAdapter;

import com.example.idachuappone.promptly.dao.CookNoDao;
import com.example.idachuappone.promptly.dao.DBHelper;
import com.example.idachuappone.promptly.entity.CookNo;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class CookNoDaoImpl implements CookNoDao {
	private Context context;
	DBHelper db;
	private SQLiteDatabase rd;
	private SQLiteDatabase wd;

	public CookNoDaoImpl(Context context) {
		this.context = context;
		this.db = new DBHelper(context);
	}

	@Override
	public List<CookNo> selectAll() {
		this.rd = this.db.getReadableDatabase();
		Cursor cursor = this.rd.rawQuery("select * from cook", null);
		List<CookNo> list = new ArrayList();
		while (cursor.moveToNext()) {
			list.add(new CookNo(cursor.getString(1), cursor.getString(BitmapCacheManagementTask.MESSAGE_FLUSH), cursor
					.getString(BitmapCacheManagementTask.MESSAGE_CLOSE), cursor
					.getString(BitmapCacheManagementTask.MESSAGE_CLEAR), cursor
					.getString(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY), cursor
					.getString(BitmapCacheManagementTask.MESSAGE_CLEAR_DISK), cursor
					.getString(BitmapCacheManagementTask.MESSAGE_CLEAR_BY_KEY), cursor
					.getString(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY), cursor
					.getString(NumericWheelAdapter.DEFAULT_MAX_VALUE)));
		}
		cursor.close();
		this.rd.close();
		return list;
	}

	@Override
	public void add(CookNo cookNo) {
		this.wd = this.db.getWritableDatabase();
		this.wd.execSQL(
				"insert into cook(id,name,head_small,location,star,bespeak_num,recname,recimg,tag) values(?,?,?,?,?,?,?,?,?)",
				new String[] { cookNo.getId(), cookNo.getName(), cookNo.getHead_small(), cookNo.getLocation(),
						cookNo.getStar(), cookNo.getBespeak_num(), cookNo.getRecname(), cookNo.getRecimg(),
						cookNo.getTag() });
		this.wd.close();
	}

	@Override
	public void deleteAll() {
		this.wd = this.db.getWritableDatabase();
		this.wd.execSQL("delete from cook");
		this.wd.close();
	}

	@Override
	public CookNo selectById(String id) {
		this.rd = this.db.getReadableDatabase();
		Cursor cursor = this.rd.rawQuery("select * from cook where id=?",
				new String[] { new StringBuilder(String.valueOf(id)).toString() });
		CookNo cookNo = null;
		if (cursor.moveToNext()) {
			cookNo = new CookNo(cursor.getString(1), cursor.getString(BitmapCacheManagementTask.MESSAGE_FLUSH),
					cursor.getString(BitmapCacheManagementTask.MESSAGE_CLOSE),
					cursor.getString(BitmapCacheManagementTask.MESSAGE_CLEAR),
					cursor.getString(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY),
					cursor.getString(BitmapCacheManagementTask.MESSAGE_CLEAR_DISK),
					cursor.getString(BitmapCacheManagementTask.MESSAGE_CLEAR_BY_KEY),
					cursor.getString(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY),
					cursor.getString(NumericWheelAdapter.DEFAULT_MAX_VALUE));
		}
		cursor.close();
		this.rd.close();
		return cookNo;
	}

	@Override
	public void deleteById(String id) {
		this.wd = this.db.getWritableDatabase();
		this.wd.execSQL("delete from cook where id=?",
				new String[] { new StringBuilder(String.valueOf(id)).toString() });
		this.wd.close();
	}
}
